package com.app.newiptv.presenter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.app.newiptv.activity.PlayerActivity;
import com.app.newiptv.base.MyApplication;
import com.app.newiptv.bean.allchannels.MainChanel;
import com.app.newiptv.bean.allchannels.Program;
import com.app.newiptv.callback.RequestCallback;
import com.app.newiptv.service.AHservice;
import com.app.newiptv.utils.APPLog;
import com.app.newiptv.utils.CacheDataPresenter;
import com.app.newiptv.utils.Paths;
import com.app.newiptv.utils.SystemTimeDate;

import java.util.List;


/**
 * Created by Shinelon on 2017/9/15.
 */

public class AllChannelsPresenter {
    private boolean isDisplayed;
    private Callback callback;
    public List<MainChanel> listContent;
    private List<Program> programList;
    private List<Program.Epg> epgList;
    private AllChannelsViewHolder viewHolder;
    private Program currentProgram;
    private int programId;
    private Activity mActivity;
    private PlayWindowPresennter.IClickPosition clickPosition;

    public AllChannelsPresenter(Activity mActivity) {
        this.mActivity = mActivity;
    }

    public void setDisplayed(boolean displayed) {
        isDisplayed = displayed;
    }

    //加载数据
    public void loadItem(Bundle params, final Callback callback) {
        this.callback = callback;
        AHservice service = new AHservice();
        String url = params.getString("url");
        service.channelAllCallback(url, new RequestCallback() {
            @Override
            public void onLoadComplete(String contentID, int pageNumber, int count, List contents) {
                if (callback == null || !isDisplayed) {
                    APPLog.printInfo("onLoadComplete:callback :");
                    return;
                }
                if (count > 0) {
                    viewHolder.setLineOneView(true);
                    listContent = contents;
                    callback.onMovieCompleted(AllChannelsPresenter.this, count);
                }
            }

            @Override
            public void onLoading(String contentID) {

            }

            @Override
            public void onFailed(String code, String msg) {

            }
        });
    }

    public void loadChannels(int positon) {
        int id = listContent.get(positon).getId();
        if (id == MyApplication.COLLECTIION) {
            List<Program> programs = CacheDataPresenter.getPresenter(mActivity).loadProgramAll();
            if (callback == null || !isDisplayed) {
                return;
            }
            programList = programs;
            if (programList.size() != 0) {
                viewHolder.setLineTwoView(true);
                callback.onChannelCompleted(AllChannelsPresenter.this, programList.size());
            }
            viewHolder.setmThreeStageRv(false);
        } else {
            AHservice service = new AHservice();
            service.channelDetailCallback(Paths.HOSTPATH + Paths.CHANNEL_PATH + "/" + id, new RequestCallback() {
                @Override
                public void onLoadComplete(String contentID, int pageNumber, int count, List contents) {
                    if (callback == null || !isDisplayed) {
                        APPLog.printInfo("onLoadComplete:callback :");
                        return;
                    }
                    programList = contents;
                    if (programList.size() != 0) {
                        viewHolder.setLineTwoView(true);
                        callback.onChannelCompleted(AllChannelsPresenter.this, programList.size());
                    }
                    viewHolder.setmThreeStageRv(false);
                }

                @Override
                public void onLoading(String contentID) {

                }

                @Override
                public void onFailed(String code, String msg) {

                }
            });
        }
    }

    public void setIClickPosition(PlayWindowPresennter.IClickPosition clickPosition) {
        this.clickPosition = clickPosition;
    }

    public void toPlayPosition(int position) {
        if (programList != null && position <= programList.size()) {
            Program program = programList.get(position);
            if (program != null) {
                if (clickPosition != null) {
                    clickPosition.callbackPosition(program);
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("type", 1002);
                    intent.putExtra("data", program);
                    intent.setClass(mActivity, PlayerActivity.class);
                    mActivity.startActivity(intent);
                    mActivity.finish();
                }
            }
        }
    }

    public void toPlayTvodProgram(int position) {
        String url = currentProgram.getReview();
//        url = "http://58.241.83.166:8081/0068/index.m3u8";
        String startTime = epgList.get(position).getStarttime();
        String endTime = epgList.get(position).getEndtime();
        Program.Epg eng = epgList.get(position);
        //http://192.168.0.39:8081/0001/index.m3u8?begin=0&end=1499003542
        String reviewUrl = url + "?begin=" + startTime + "&end=" + endTime;
        APPLog.printInfo("reviewUrl==>:" + reviewUrl);
        if (url == null) {
            Toast.makeText(mActivity, "暂无回看内容", Toast.LENGTH_SHORT).show();
            return;
        }
        if (clickPosition != null) {
            clickPosition.lookBackCallback(url, programId, epgList, position);
        }
//        Intent intent = new Intent();
//        intent.putExtra("type", 1003);
//        intent.putExtra("data", eng);
//        intent.putExtra("playUrl", reviewUrl);
//        intent.setClass(mActivity, PlayerActivity.class);
//        mActivity.startActivity(intent);
    }

    public void loadProgram(int position) {
        epgList = programList.get(position).getEpg();
        programId = programList.get(position).getId();
        currentProgram = programList.get(position);
        if (epgList != null) {
            callback.onProgramCompleted(AllChannelsPresenter.this, epgList.size());
        }
        viewHolder.setmThreeStageRv(true);
    }

    public void onBindMainChanelView(MainViewHolder holder, int position) {
        if (!isDisplayed) {
            return;
        }
        holder.setmainChanelName(listContent.get(position).getName());
    }

    public void onBindChannel(SencondViewHolder viewHolder, int position) {
        if (isDisplayed) {
            viewHolder.setChannel(programList.get(position).getName());
        }
    }

    public void onBindProgram(ProgramViewHolder viewHolder, int position) {
        if (!isDisplayed) {
            return;
        }
        long time = System.currentTimeMillis();
        viewHolder.setTitleText(epgList.get(position).getTitle());
        viewHolder.setStartTimeText(SystemTimeDate.stampToMMddDate(epgList.get(position).getStarttime()));
        viewHolder.setEndTimeText(SystemTimeDate.stampToHHmmDate(epgList.get(position).getStarttime()) + " - " + SystemTimeDate.stampToHHmmDate(epgList.get(position).getEndtime()));
        long startLt = new Long(epgList.get(position).getStarttime());
        long endLt = new Long(epgList.get(position).getEndtime());
        startLt *= 1000;
        endLt *= 1000;
        if (time >= startLt && time <= endLt) {
            viewHolder.setTypeBtnText("直播", 1);
        } else if (time > endLt) {
            viewHolder.setTypeBtnText("回看", 0);
        } else if (time < startLt) {
            viewHolder.setTypeBtnText("预约", 0);
        }
    }

    public void onBindAllChannels(AllChannelsViewHolder viewHolder) {
        this.viewHolder = viewHolder;

    }

    public interface Callback {
        void onError(int code, String msg);

        void onMovieCompleted(AllChannelsPresenter presenter, int count);

        void onChannelCompleted(AllChannelsPresenter presenter, int count);

        void onProgramCompleted(AllChannelsPresenter presenter, int count);
    }

    public interface MainViewHolder {
        void setmainChanelName(String name);
    }

    public interface SencondViewHolder {
        void setChannel(String name);
    }

    public interface ProgramViewHolder {
        void setTitleText(String name);

        void setTypeBtnText(String name, int type);

        void setStartTimeText(String name);

        void setEndTimeText(String name);

    }

    public interface AllChannelsViewHolder {
        void setmThreeStageRv(boolean isShow);

        void setLineTwoView(boolean isShow);

        void setLineOneView(boolean isShow);
    }
}
